# 106. 正则问题
# https://www.lanqiao.cn/problems/106/learning/?page=1&first_category_id=1&second_category_id=3&contain_answer=true
# Date:2025/2/7

def longest_match_length(reg: str):
    reg = "(" + reg + ")"  # 在最外层添加一个括号, 方便直接计算长度
    stack = []
    for c in reg:
        if c != ')':
            stack.append(c)
        else:
            str_part = ""
            # 截取括号中的有效的字符串
            while stack[-1] != "(":
                str_part += stack.pop()
            stack.pop()  # 弹出左括号
            # 抽取有效的字符串长度部分
            parts = str_part.split("|")
            max_length = max(map(len, parts))  # 有效字符串可以匹配的最大长度
            stack.append("x" * max_length)

    return len(stack[0])


if __name__ == '__main__':
    # inp = input()
    inp = "((xx|xxx)x|(x|xx))xx"
    print(longest_match_length(inp))  # 6
    inp = "(x)x|(x)(x)"
    print(longest_match_length(inp))  # 2
